class Solution(object):
    def numberOfWays(self, s):
        n = len(s)
        pre = [0] * n
        if s[0] == '1':
            pre[0] = 1
        for i in range(1, n):
            pre[i] = pre[i - 1] + (s[i] == '1')
        ans = 0
        for i in range(1, n - 1):
            if s[i] == '1':
                ans += (i - pre[i - 1]) * (n - 1 - i - (pre[-1] - pre[i]))
            else:
                ans += pre[i - 1] * (pre[-1] - pre[i])
        return ans
